﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Obj;
using BLL;

public partial class AreaRestrita_Modelo : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ValidarUsuario();
        if (!IsPostBack)
        {
            List<objModelo> lista = new List<objModelo>();
            BLLModelo regra = new BLLModelo();
            lista = regra.lstModelo();
            gdvModelo.DataSource = lista;
            gdvModelo.DataBind();
        }
    }
    protected void btnNovo_Click(object sender, EventArgs e)
    {
        Response.Redirect("ModeloForm.aspx", true);
    }
    protected void btnFiltrar_Click(object sender, EventArgs e)
    {
        objModelo Modelo = new objModelo(txtModelo.Text);
        BLLModelo Regra = new BLLModelo();

        List<objModelo> Modelos = Regra.lstModelo(Modelo);

        gdvModelo.DataSource = Modelos;
        gdvModelo.DataBind();
    }
    protected void gdvModelo_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Excluir")
        {
            BLLAutomovel RegraAutomovel = new BLLAutomovel();
            objAutomovel automovel = new objAutomovel();
            automovel.ModeloID = Convert.ToInt32(e.CommandArgument);

            List<objAutomovel> automoveis = RegraAutomovel.lstAutomovel(automovel);

            if (automoveis.Count == 0)
            {

                BLLModelo regra = new BLLModelo();
                objModelo Modelo = new objModelo(Convert.ToInt32(e.CommandArgument));
                regra.Save(Modelo, action.DELETE);

                ScriptManager.RegisterClientScriptBlock(
                this,
                typeof(Page),
                "ToggleScript",
                "alert('Modelo excluído com sucesso!');window.location.href='Modelo.aspx'",
                true);
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(
                this,
                typeof(Page),
                "ToggleScript",
                "alert('Não é possível excluir. Existe um automóvel cadastrado para este Modelo!');window.location.href='Modelo.aspx'",
                true);
            }
        }
        else if (e.CommandName == "Editar")
        {
            Session["ModeloID"] = e.CommandArgument;
            Response.Redirect("ModeloForm.aspx", true);
        }
    }
    protected void gdvModelo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            objModelo Modelo = (objModelo)e.Row.DataItem;
            ImageButton imgEditar = (ImageButton)e.Row.FindControl("imgEditar");
            imgEditar.CommandArgument = Modelo.ModeloID.ToString();

            ImageButton imgExcluir = (ImageButton)e.Row.FindControl("imgExcluir");
            imgExcluir.CommandArgument = Modelo.ModeloID.ToString();
        }
    }
}